package com.example.drainage.ui.downwin

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.core.graphics.toColorInt
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.arm.base.BaseActivity
import com.example.arm.ext.dpInt
import com.example.drainage.R
import com.example.drainage.base.ktexpand.setBackgroundByDrawable
import com.example.drainage.databinding.ActivityDownWinBinding
import com.example.drainage.databinding.ItemCertSupervisionWorkOrderBinding
import java.util.*

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.downwin
 * @createTime 创建时间 ：2021/12/9
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class DownWinActivity : BaseActivity<ActivityDownWinBinding>() {

    companion object {

    }

    override fun initView(savedInstanceState: Bundle?): ActivityDownWinBinding {
        return ActivityDownWinBinding.inflate(layoutInflater)
    }

    override fun initData(savedInstanceState: Bundle?) {
        updateView()
    }

    private fun updateView() {
        val downWinHelper = DownWinHelper(binding)
        downWinHelper.conditionChangeListener = { month: String, district: String, distance: String, grantTimeSort: Boolean ->
            Log.d("-11-", "--$month--$district--$distance--$grantTimeSort")
            binding.statisticsPsh1.tvItemTips.text = "${month.split("-")[1]}月应监管"
        }
        binding.apply {
            pshTab.setBackgroundByDrawable(3.dpInt, fillColor = "#FFF1F2F5".toColorInt())

            tvLastMonthCreate.setOnClickListener {
                selectPshTab(tvAllPsh, tvLastMonthCreate)
            }
            tvAllPsh.setOnClickListener {
                selectPshTab(tvLastMonthCreate, tvAllPsh)
            }
            selectPshTab(tvAllPsh, tvLastMonthCreate)

            statisticsPsh1.root.setBackgroundByDrawable(3.dpInt, fillColor = Color.WHITE)
            statisticsPsh2.root.setBackgroundByDrawable(3.dpInt, fillColor = Color.WHITE)
            statisticsPsh3.root.setBackgroundByDrawable(3.dpInt, fillColor = Color.WHITE)
            statisticsPsh4.root.setBackgroundByDrawable(3.dpInt, fillColor = Color.WHITE)

            val curMonth = Calendar.getInstance().get(Calendar.MONTH) + 1
            statisticsPsh1.tvItemTips.text = "${curMonth}月应监管"
            statisticsPsh2.tvItemTips.text = "当月监管"
            statisticsPsh3.tvItemTips.text = "已监管"
            statisticsPsh4.tvItemTips.text = "未监管"

            statisticsPsh1.tvItemValue.text = "88户"
            statisticsPsh2.tvItemValue.text = "188户"
            statisticsPsh3.tvItemValue.text = "288户"
            statisticsPsh4.tvItemValue.text = "388户"

            val adapter = CertSupervisionWorkOrderAdapter()
            adapter.setNewInstance(
                mutableListOf(
                    CertSupervisionWorkOrderBean(
                        "维也纳酒店维也纳酒店维也纳酒店维也纳酒店维也纳酒店维也纳酒店维也纳酒店维也纳酒店",
                        0, "镇街：石滩镇", "发证日期：2021-08-05", "距离：180米", "最后监管时间：2021-08-05", 0
                    ),
                    CertSupervisionWorkOrderBean("维也纳酒店", 1, "镇街：石滩镇", "发证日期：2021-08-05", "距离：180米", "最后监管时间：2021-08-05", 1),
                    CertSupervisionWorkOrderBean("万科广场米酷", 1, "镇街：石滩镇", "发证日期：2021-08-05", "距离：180米", "最后监管时间：2021-08-05", 0),
                    CertSupervisionWorkOrderBean("凌塘村小学", 2, "镇街：石滩镇", "发证日期：2021-08-05", "距离：180米", "最后监管时间：2021-08-05", 1),
                    CertSupervisionWorkOrderBean("凌塘村小学", 2, "镇街：石滩镇", "发证日期：2021-08-05", "距离：180米", "最后监管时间：2021-08-05", 1),
                )
            )
            recyclerViewContent.adapter = adapter
        }
    }

    private fun selectPshTab(unSelectView: TextView, selectView: TextView) {
        unSelectView.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        unSelectView.background = null

        selectView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        selectView.setBackgroundByDrawable(3.dpInt, fillColor = "#FFFBFBFC".toColorInt())
    }

    data class CertSupervisionWorkOrderBean(
        val name: String,
        val type: Int,
        val street: String,
        val grantTime: String,
        val distance: String,
        val lastTime: String,
        val state: Int
    )

    class CertSupervisionWorkOrderAdapter :
        BaseQuickAdapter<CertSupervisionWorkOrderBean, BaseViewHolder>(R.layout.item_cert_supervision_work_order) {
        override fun convert(holder: BaseViewHolder, item: CertSupervisionWorkOrderBean) {
            ItemCertSupervisionWorkOrderBinding.bind(holder.itemView).apply {
                root.setBackgroundByDrawable(3.dpInt, fillColor = Color.WHITE)
                tvPshType.text = getTypeStr(item.type)
                tvPshType.setTextColor(getTypeColor(item.type))
                tvPshType.setBackgroundByDrawable(2.dpInt, 1.dpInt, getTypeColor(item.type))

                tvPshName.text = item.name
                tvStreet.text = item.street
                tvDistance.text = item.distance
                tvGrantTime.text = item.grantTime
                tvLastTime.text = item.lastTime

                ivState.setImageResource(getStateResId(item.state))
            }
        }

        private fun getTypeColor(type: Int): Int {
            return when (type) {
                0 -> "#FFF7405A".toColorInt()
                1 -> "#FFFDA22D".toColorInt()
                else -> "#FF9066F8".toColorInt()
            }
        }

        private fun getTypeStr(type: Int): String {
            return when (type) {
                0 -> "重点一类"
                1 -> "重点二类"
                else -> "一般"
            }
        }

        private fun getStateResId(state: Int): Int {
            return when (state) {
                0 -> R.mipmap.icon_cert_work_order_state_supervised
                else -> R.mipmap.icon_cert_work_order_state_unregulated
            }
        }
    }
}